#ifdef __WII__
#include <stdio.h>
#include <stdlib.h>
#include "lib\wiiuse.h"
#include "Pong3D.h"


#define NUMBER_OF_REMOTES 1

wiimote**  remote;
 int time = 0;
/*
Function Name: handle_event
Description: If any change occurs in the wiimote state such as button press or movement
			this method is called to process the events and call relevant callback methods 
Inputs: Pointer to the Wiimote instance that needs to be handled
returns:	null
*/
void handle_event(struct wiimote_t* rm){
	int i;
	int normalizedX, normalizedY;
    if(IS_PRESSED(rm, WIIMOTE_BUTTON_UP)){
        printf("\n - IR Activated - \n");
        wiiuse_set_ir(rm,1);
    }
    else if(IS_PRESSED(rm, WIIMOTE_BUTTON_DOWN)){
        printf("\n - IR Dectivated - \n");
        wiiuse_set_ir(rm,0);
    }
	else if(IS_JUST_PRESSED(rm, WIIMOTE_BUTTON_A)){
		keyboard(' ', 0, 0);
	}

    if(WIIUSE_USING_IR(rm)){
		
        for(i=0; i<4; i++){
            if(rm->ir.dot[i].visible){
              normalizedX = (int)(((float)rm->ir.dot[i].x/1024.0) * width);
			normalizedY = (int)(((float)rm->ir.dot[i].y/768.0) * height); 
			passiveMotion(normalizedX, normalizedY);
			
				 printf("IR source %i: (%u, %u)\n", i, rm->ir.dot[i].x, rm->ir.dot[i].y);
				 printf("Normalized Value: (%i, %i)\n", normalizedX, normalizedY);
            }

			
			if(time == 100)
			{
				
				  printf("IR cursor: (%u, %u)\n", rm->ir.x, rm->ir.y);
				  time = 0;
			}

			time++;
           
			
			
            //printf("IR z distance: %f\n", rm->ir.z);

        }
    }
}

/*
Function Name: handle_disconnect
Description: Handles untimely disconnection of the wii mote 
Inputs: Pointer to the Wiimote instance that needs to be handled
returns:	null
*/
void handle_disconnect(struct wiimote_t* rm){
    printf("\n - DISCONNECTED - ID: %i\n\n", rm->unid);
}


int connect_wii()
{
	int found, connected;
    remote = wiiuse_init(NUMBER_OF_REMOTES);
    found = wiiuse_find(remote, NUMBER_OF_REMOTES, 5000);
    connected = wiiuse_connect(remote, found);
	printf("Searching...");
	printf("Found %d devices\n", found);
    if(!connected){
        printf("Failed to connect\n");
        return 0;
    }
    else{

        printf("Connected\n");
        wiiuse_rumble(remote[0],1);
        Sleep(250);
        wiiuse_rumble(remote[0],0);        
    }
}

void poll_wii(){
	int i;
	if (wiiuse_poll(remote, NUMBER_OF_REMOTES)) {
                for(i=0;i<NUMBER_OF_REMOTES; i++){
                    switch(remote[i]->event){
                        case WIIUSE_EVENT:
                                   handle_event(remote[i]); break;

                        case WIIUSE_DISCONNECT:
                        case WIIUSE_UNEXPECTED_DISCONNECT:
                                   handle_disconnect(remote[i]); break;
                        default: break;
                    }
                }
         }
}

/*
Function Name: disconnect_wii
Description: Handles timely disconnection of the wii mote 
Inputs: null
returns:	null
*/
void disconnect_wii(){
	wiiuse_cleanup(remote,NUMBER_OF_REMOTES);
}
#endif